package leo;

import java.util.Date;
import java.util.List;

import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

public class SchedulerLeo {

	public SchedulerLeo() {
		System.out.println("### SchedulerLeo... " + new Date());
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched;
		try {
			sched = sf.getScheduler();
			sched.start();
			JobDetail jd = new JobDetail("sillyJob", sched.DEFAULT_GROUP,
					SillyJob.class);
			SimpleTrigger st = new SimpleTrigger("mytrigger",
					sched.DEFAULT_GROUP, new Date(), null,
					SimpleTrigger.REPEAT_INDEFINITELY, 60L * 1000L);
			
			sched.scheduleJob(jd, st);
			//########### A cada minuto... ####
			//JobDetail genericJob = new JobDetail("job01", sched.DEFAULT_GROUP, GenericJob.class);
			//CronTrigger ct = new CronTrigger("tr01", sched.DEFAULT_GROUP, "0/30 * * * * ?"); // every 30 secs
			
			sched.scheduleJob(new JobDetail("job01", sched.DEFAULT_GROUP, GenericJob.class) 
				, new CronTrigger("tr01", sched.DEFAULT_GROUP, "0/30 * * * * ?"));
			
			sched.scheduleJob(new JobDetail("job02", sched.DEFAULT_GROUP, GenericJob.class) 
			, new CronTrigger("tr02", sched.DEFAULT_GROUP, "0/25 * * * * ?"));
			
			sched.addJob(new JobDetail("job02", sched.DEFAULT_GROUP, GenericJob.class), true);
			
			//##################################
			
			//CronTrigger trigger  new CronTrigger("Income Report", "Report Generation");
			//trigger.setCronExpression("0 0 12 ? * SUN"); // "0/30 * * * * ?"
			//sched.scheduleJob(jobDetail, trigger);
			
			
			List jobs = sched.getCurrentlyExecutingJobs();			
			System.out.println("### After Creation - Jobs: " + jobs);
			
			//System.out.println("### getGlobalJobListeners: " +sched.getGlobalJobListeners() );
			for (Object obj : sched.getGlobalJobListeners()) {
				System.out.println("### getGlobalJobListeners: " + obj);
			}
			
			//System.out.println("### getJobGroupNames: " +sched.getJobGroupNames() );
			for (Object obj : sched.getSchedulerListeners()) {
				System.out.println("### getGlobalJobListeners: " + obj);
			}
			
			for (String obj : sf.getScheduler().getJobNames(sched.DEFAULT_GROUP)) {
				System.out.println("### getJobs: " + obj);
				//sched.getJobDetail(obj, sched.DEFAULT_GROUP)
				//sched.de
			}
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
